package hupu

/**
 * 处理 hupu nba 数据的函数包
 */

 import (
 	_ "github.com/go-sql-driver/mysql"
 	"github.com/go-xorm/xorm"
 	"fmt"
 )


//大写开头就会在表名上加 _ 区别
type HupuNbaTeam struct {
	Id int64		//自增ID
	Name string		//名称
	Win		int 	//胜次数
	Lost	int   //负次数
	Area	string  //区域　west西 east东
	Zone	string  //赛区　西南赛区 ,　太平洋赛区
	Url		string	//远程下载地址
	Img 	string	//图片地址
}




var engine *xorm.Engine


//初始化数据库
func InitDb(){
	var err error
	engine,err = xorm.NewEngine("mysql","root:@/go2015?charset=utf8")
	if err != nil{
		fmt.Println(err)
		return
	}
	defer engine.Close()
	engine.ShowSQL = true
	err = engine.Sync(new(HupuNbaTeam))

	// err =  engine.CreateTables(&HupuNbaTeam{})

	if err != nil{
		fmt.Println(err)
		return
	}
}





/**
 * 插入球队
 *
 *	name 球队名 地区　赛区 图片 hupu网址 win 胜数  lost 失败数
 */
func InsertTeam(name,area,zone,url,img string,win,lost int) bool{
	var err error
	engine,err = xorm.NewEngine("mysql","root:@/go2015?charset=utf8")
	if err != nil{
		fmt.Println(err)
		return false
	}
	defer engine.Close()

	team := new(HupuNbaTeam)
	team.Name = name
	team.Win = win
	team.Lost = lost
	team.Area = area
	team.Zone = zone
	team.Url = url 
	team.Img = img
	affected,err := engine.Insert(team)
	if(err != nil){
		fmt.Println(err)
		return false
	}
	if affected > 0 {
		return true
	}else{
		return false
	}
	
}